<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 今汇通
 */
class DingfengController extends PayController{
    protected $config;
    protected $class;

    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['return_type'] = 'json';
        $data['api_code'] = $this->config['mch_id'];
        switch($this->config['paytype']) {
            case 1:
                $data['is_type'] = 'wechatzjnx';
                break;
            //支付宝
            case 3:
            case 4:
            case 17:
            case 18:
                $data['is_type'] = 'alipayai';
                break;
            default:
                $this->showmessage('暂不支持此支付方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['price'] = number_format($return['amount'],2,'.','');
        $data['order_id'] = $return['orderid'];
        $data['time'] = time();
        $data['mark'] = 'fishing';
        $data['return_url'] = $return['callbackurl'];
        $data['notify_url'] = $return['notifyurl'];

        ksort($data);

        $str = httpstr($data) . "&key=".$this->config['signkey'];
        $data['sign'] = strtoupper(md5($str));

        $res = curl($this->config['gateway'], httpstr($data));

        $res = json_decode($res, true);

        //echo '<pre/>'; print_r($data); print_r($res); echo '请求地址：'.$this->config['gateway']; exit;

        $code = $res['payurl'];
        if(!$code){
            if($res['messages']['returnmsg']){
                $this->showmessage($res['messages']['return_msg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            if($res['returnmsg']){
                $this->showmessage($res['returnmsg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //echo $code;exit;

        //header("Location:{$code}"); exit;

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        //$this->assign("code_url", $res['data']['payurl']);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));

        switch($this->config['paytype']){
            case 1:

                break;
            //支付宝扫码
            case 3:
                //支付宝H5
            case 4:
            case 17:
            case 18:
                if(isMobile()){
                    header("Location:{$code}"); exit;
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(), $this->yinglianip())){
            //exit("wrong");
        }

        $p = $origin = file_get_contents('php://input');

        $p = json_decode($p, true);

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['order_id'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['paysapi_id'])
        ));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        unset($p['sign']);

        ksort($p);

        $str = httpstr($p) . '&key='.$this->config['signkey'];
        $sign2 = strtoupper(md5($str));
        if($sign != $sign2){
            exit("sign_wrong");
        }

        if($p['code'] !== 1){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}